package handlers;

import java.net.Socket;

import common.classes.FileNameWorker;
import common.classes.FileResponseThread;
import common.classes.HeaderWorker;


public class GetHandler extends Handler {
	private String path;
	
	public GetHandler() {
		super();
		this.path = "datas/";
	}

	public GetHandler(Handler next) {
		super(next);
		this.path = "datas/";
	}

	public GetHandler(Handler next, String path) {
		super(next);
		this.path = path;
	}

	@Override
	public void parseRequest(Socket socket, HeaderWorker header, String error) {
		if (error != null) {
			super.parseRequest(socket, header, error);
			return;
		}
		if (!header.getMethod().equals("GET")) {
			super.parseRequest(socket, header, "400 Bad Request");
			return;
		}
			
		String filePath = FileNameWorker.createFullFilePath(this.path, header.getFilePath());
		if (filePath.equals("")) {
			super.parseRequest(socket, null, "404 Not Found");
			return;
		}
		
		Thread th = new Thread(new FileResponseThread(socket, filePath, header));
		th.start();
	}
}